package com.wiz.web.controllor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wiz.entity.User;
import com.wiz.service.HeadquartersService;
import com.wiz.service.NoteService;
import com.wiz.service.UserService;
import com.wiz.to.HQBaseInfoTO;
import com.wiz.util.PageView;
import com.wiz.vo.NoteVO;
import com.wiz.vo.ResponseJsonBody;

/**
 * 短信
 * @author xxy
 *
 */
@Controller
@RequestMapping("/note")
public class NoteControllor {
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	private transient final Logger log=LoggerFactory.getLogger(getClass());
	@Resource
	private HeadquartersService headquartersService;
	@Resource
	private UserService userService;
	@Resource
	private NoteService noteService;
	
	/**
	 * 跳转到充值页面
	 */
	@RequestMapping(value="/toCharge")
	public String toCharge(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "hqId", required = true) String hqId,//总部id
			HttpSession session, Model model) {
		HQBaseInfoTO hqBaseInfoTO = null;
		try {
			hqBaseInfoTO = headquartersService.gethqsById(hqId);
			model.addAttribute("hqBaseInfoTO", hqBaseInfoTO);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return "note/charge";
		
	}
	@RequestMapping(value="/charge")
	@ResponseBody
	public String charge(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "hqId", required = true) String hqId,//总部id
			@RequestParam(value = "addCnt", required = true) int addCnt,//充值条数
			@RequestParam(value = "amount", required = true) Double amount,//充值金额
    		HttpSession session, Model model) {
		ResponseJsonBody json = null;
		try {
			UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			User users = this.userService.querySingleUser(userDetails.getUsername());
			
			NoteVO note = new NoteVO(hqId, addCnt, amount, users.getId(), users.getName());
			
			noteService.charge(note);
			 json = new ResponseJsonBody("200", "充值成功", "hq_list",
						"navTab", "closeCurrent", null, null);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			json = new ResponseJsonBody("300", e.getMessage(), "hq_list",
					"navTab", "closeCurrent", null, null);
		}
		return gson.toJson(json);
		
	}
	/**
	 * 发送短信记录
	 */
	@RequestMapping(value="/listSendHistory")
	public String listSendHistory(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "numPerPage", required = false,defaultValue="10") int pageSize,
			@RequestParam(value = "pageNum", required = false,defaultValue="1") int pageNow,
			@RequestParam(value = "hqId", required = false) String hqId,
			@RequestParam(value = "startTime", required = false) String startTime,
			@RequestParam(value = "endTime", required = false) String endTime,
			HttpSession session, Model model) {
		
		try {
			PageView pageView = new PageView(pageSize, pageNow);
			pageView = noteService.listSendHistory(hqId, pageView,startTime,endTime);
			model.addAttribute("pageView", pageView);
			model.addAttribute("hqId", hqId);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		
		return "note/listSendHistory";
	}
	
	@RequestMapping(value="/listChargeHistory")
	public String listChargeHistory(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "numPerPage", required = false,defaultValue="10") int pageSize,
			@RequestParam(value = "pageNum", required = false,defaultValue="1") int pageNow,
			@RequestParam(value = "hqId", required = false) String hqId,
			@RequestParam(value = "startTime", required = false) String startTime,
			@RequestParam(value = "endTime", required = false) String endTime,
    		HttpSession session, Model model) {
		
		try {
			PageView pageView = new PageView(pageSize, pageNow);
			pageView = noteService.listChargeHistory(hqId, pageView,startTime,endTime);
			model.addAttribute("pageView", pageView);
			model.addAttribute("hqId", hqId);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		
		return "note/listChargeHistory";
	}
	
}
